﻿using GC_FFW.BusinessEntities.Abstractions.Interfaces.Forecast;
using GC_FFW.BusinessEntities.Forecast.Xml;
using GC_FFW.ForecastWriter.Abstractions.Adapters;
using GC_FFW.ForecastWriter.Abstractions.Interfaces.Helpers;
using GC_FFW.ForecastWriter.Common;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace GC_FFW.ForecastWriter.Adapters.TeamMatch.LastMatches
{
	public class AwayTeamResultsProposalAdapter : ProposalBaseAdapter
	{
		#region Constructors

		public AwayTeamResultsProposalAdapter(IForecastData forecastData, Conditions conditions,
			IStringManager stringManager, IRandomizer randomizer)
			: base(forecastData, conditions, stringManager, randomizer) { }

		#endregion

		#region Overrides

		public override string GetText()
		{
			if (ForecastData.AwayTeam == null || ForecastData.AwayTeam.LastMatches == null || !ForecastData.AwayTeam.LastMatches.Any())
				return string.Empty;

			var result = string.Empty;

			if (ForecastData.AwayTeam.LesionsCount == 0 && ForecastData.AwayTeam.DrawsCount == 0)
			{
				var proposal = GetProposal(Conditions.GetCondition(1));
				if (proposal == null) return string.Empty;

				result = proposal.Text ?? string.Empty;
				if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

				var dictionary = new Dictionary<string, string>();
				foreach (var keyword in proposal.KeywordList)
				{
					switch (keyword)
					{
						case Constants.AWAY_TEAM_NAME_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_NAME_KEYWORD,
								ForecastData.AwayTeam.Name ?? string.Empty);

							break;

						case Constants.COUNT_MATCHES_KEYWORD:

							dictionary.Add(
								Constants.COUNT_MATCHES_KEYWORD,
								ForecastData.AwayTeam.LastMatches.Count().ToString(CultureInfo.InvariantCulture));

							break;
					}
				}

				result = StringManager.ReplaceKeywords(result, dictionary);
			}
			else if (ForecastData.AwayTeam.LesionsCount == 0 && ForecastData.AwayTeam.DrawsCount > 0
				&& ForecastData.AwayTeam.WinsCount > 0)
			{
				var proposal = GetProposal(Conditions.GetCondition(2));
				if (proposal == null) return string.Empty;

				result = proposal.Text ?? string.Empty;
				if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

				var dictionary = new Dictionary<string, string>();
				foreach (var keyword in proposal.KeywordList)
				{
					switch (keyword)
					{
						case Constants.AWAY_TEAM_NAME_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_NAME_KEYWORD,
								ForecastData.AwayTeam.Name ?? string.Empty);

							break;

						case Constants.COUNT_MATCHES_KEYWORD:

							dictionary.Add(
								Constants.COUNT_MATCHES_KEYWORD,
								ForecastData.AwayTeam.LastMatches.Count().ToString(CultureInfo.InvariantCulture));

							break;

						case Constants.AWAY_TEAM_WINS_COUNT_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_WINS_COUNT_KEYWORD,
								ForecastData.AwayTeam.WinsCount.ToString(CultureInfo.InvariantCulture));

							break;

						case Constants.VICTORY_WORD_KEYWORD:

							dictionary.Add(
								Constants.VICTORY_WORD_KEYWORD,
								StringManager.GetSyntacticKeywordValue(Constants.VICTORY_WORD_KEYWORD, ForecastData.AwayTeam.WinsCount));

							break;

						case Constants.DRAWS_COUNT_KEYWORD:

							dictionary.Add(
								Constants.DRAWS_COUNT_KEYWORD,
								ForecastData.AwayTeam.DrawsCount.ToString(CultureInfo.InvariantCulture));

							break;

						case Constants.MATCH_WORD_KEYWORD:

							dictionary.Add(
								Constants.MATCH_WORD_KEYWORD,
								StringManager.GetSyntacticKeywordValue(Constants.MATCH_WORD_KEYWORD, ForecastData.AwayTeam.DrawsCount));

							break;

						case Constants.COMPLETED_WORD_KEYWORD:

							dictionary.Add(
								Constants.COMPLETED_WORD_KEYWORD,
								StringManager.GetSyntacticKeywordValue(Constants.COMPLETED_WORD_KEYWORD, ForecastData.AwayTeam.DrawsCount));

							break;
					}
				}

				result = StringManager.ReplaceKeywords(result, dictionary);
			}
			else if (ForecastData.AwayTeam.DrawsCount == ForecastData.AwayTeam.LastMatches.Count())
			{
				var proposal = GetProposal(Conditions.GetCondition(3));
				if (proposal == null) return string.Empty;

				result = proposal.Text ?? string.Empty;
				if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

				var dictionary = new Dictionary<string, string>();
				foreach (var keyword in proposal.KeywordList)
				{
					switch (keyword)
					{
						case Constants.AWAY_TEAM_NAME_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_NAME_KEYWORD,
								ForecastData.AwayTeam.Name ?? string.Empty);

							break;

						case Constants.COUNT_MATCHES_KEYWORD:

							dictionary.Add(
								Constants.COUNT_MATCHES_KEYWORD,
								ForecastData.AwayTeam.LastMatches.Count().ToString(CultureInfo.InvariantCulture));

							break;
					}
				}

				result = StringManager.ReplaceKeywords(result, dictionary);
			}
			else if (ForecastData.AwayTeam.WinsCount > 0 && ForecastData.AwayTeam.LesionsCount > 0)
			{
				var proposal = GetProposal(Conditions.GetCondition(4));
				if (proposal == null) return string.Empty;

				result = proposal.Text ?? string.Empty;
				if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

				var dictionary = new Dictionary<string, string>();
				foreach (var keyword in proposal.KeywordList)
				{
					switch (keyword)
					{
						case Constants.AWAY_TEAM_NAME_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_NAME_KEYWORD,
								ForecastData.AwayTeam.Name ?? string.Empty);

							break;

						case Constants.COUNT_MATCHES_KEYWORD:

							dictionary.Add(
								Constants.COUNT_MATCHES_KEYWORD,
								ForecastData.AwayTeam.LastMatches.Count().ToString(CultureInfo.InvariantCulture));

							break;

						case Constants.LESIONS_COUNT_KEYWORD:

							dictionary.Add(
								Constants.LESIONS_COUNT_KEYWORD,
								ForecastData.AwayTeam.LesionsCount.ToString(CultureInfo.InvariantCulture));

							break;

						case Constants.ONCE_WORD_KEYWORD:

							dictionary.Add(
								Constants.ONCE_WORD_KEYWORD,
								StringManager.GetSyntacticKeywordValue(Constants.ONCE_WORD_KEYWORD, ForecastData.AwayTeam.LesionsCount));

							break;

						case Constants.AWAY_TEAM_WINS_COUNT_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_WINS_COUNT_KEYWORD,
								ForecastData.AwayTeam.WinsCount.ToString(CultureInfo.InvariantCulture));

							break;
					}
				}

				result = StringManager.ReplaceKeywords(result, dictionary);
			}
			else if (ForecastData.AwayTeam.WinsCount == 0 && ForecastData.AwayTeam.LesionsCount > 0
				&& ForecastData.AwayTeam.DrawsCount > 0)
			{
				var proposal = GetProposal(Conditions.GetCondition(5));
				if (proposal == null) return string.Empty;

				result = proposal.Text ?? string.Empty;
				if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

				var dictionary = new Dictionary<string, string>();
				foreach (var keyword in proposal.KeywordList)
				{
					switch (keyword)
					{
						case Constants.AWAY_TEAM_NAME_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_NAME_KEYWORD,
								ForecastData.AwayTeam.Name ?? string.Empty);

							break;

						case Constants.COUNT_MATCHES_KEYWORD:

							dictionary.Add(
								Constants.COUNT_MATCHES_KEYWORD,
								ForecastData.AwayTeam.LastMatches.Count().ToString(CultureInfo.InvariantCulture));

							break;

						case Constants.LESIONS_COUNT_KEYWORD:

							dictionary.Add(
								Constants.LESIONS_COUNT_KEYWORD,
								ForecastData.AwayTeam.LesionsCount.ToString(CultureInfo.InvariantCulture));

							break;

						case Constants.MATCH_WORD_KEYWORD:

							dictionary.Add(
								Constants.MATCH_WORD_KEYWORD,
								StringManager.GetSyntacticKeywordValue(Constants.MATCH_WORD_KEYWORD, ForecastData.AwayTeam.LesionsCount));

							break;
					}
				}

				result = StringManager.ReplaceKeywords(result, dictionary);
			}
			else if (ForecastData.AwayTeam.LesionsCount == ForecastData.AwayTeam.LastMatches.Count())
			{
				var proposal = GetProposal(Conditions.GetCondition(5));
				if (proposal == null) return string.Empty;

				result = proposal.Text ?? string.Empty;
				if (proposal.KeywordList == null || !proposal.KeywordList.Any()) return result;

				var dictionary = new Dictionary<string, string>();
				foreach (var keyword in proposal.KeywordList)
				{
					switch (keyword)
					{
						case Constants.AWAY_TEAM_NAME_KEYWORD:

							dictionary.Add(
								Constants.AWAY_TEAM_NAME_KEYWORD,
								ForecastData.AwayTeam.Name ?? string.Empty);

							break;

						case Constants.COUNT_MATCHES_KEYWORD:

							dictionary.Add(
								Constants.COUNT_MATCHES_KEYWORD,
								ForecastData.AwayTeam.LastMatches.Count().ToString(CultureInfo.InvariantCulture));

							break;
					}
				}

				result = StringManager.ReplaceKeywords(result, dictionary);
			}

			return result;
		}

		#endregion
	}
}
